package com.yanggu.mapreduce.groupingcomparator;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @version V1.0
 * @author: YangGu
 * @date: 2020/2/7 16:37
 * @description:
 */
public class OrderBeanDriver {

    public static void main(String[] args) throws Exception {
        args = new String[]{"input/groupingcomparator", "output/groupingcomparator"};

        //1. 获取job对象
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        //2. 设置关联driver、mapper、reduce类
        job.setJarByClass(OrderBeanDriver.class);
        job.setMapperClass(OrderBeanMapper.class);
        job.setReducerClass(OrderBeanReducer.class);

        //3. 设置map的输出的key和value的类型
        job.setMapOutputKeyClass(OrderBean.class);
        job.setMapOutputValueClass(NullWritable.class);

        //4. 设置最终的输出的key和value的类型
        job.setOutputKeyClass(OrderBean.class);
        job.setOutputValueClass(NullWritable.class);

        //设置分组
        job.setGroupingComparatorClass(OrderBeanGroupingComparator.class);

        //5. 设置输入路径和输出的路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //6. 提交job
        boolean result = job.waitForCompletion(true);

        System.exit(result ? 0 : 1);
    }

}
